import requests

# from  requests import Session
# # 发送http请求并接受返回的响应
# rp=requests.get(url='http://127.0.0.1:8080/pinter')
#缺点：不支持自动处理cookie
# # 打印 响应的原始数据
# print(type(rp.text))
# print(rp.text)



#定义全局变量sess
sess=requests.Session()
IP= '192.168.1.180'

#Session 是一个会话对象，可以自动处理cookie
rp=sess.request("get", url="http://" + IP + ":8080/pinter")
#requests.Session()的request方法支持自动处理cookie
print(type(rp.text))
print(rp.text)
print(rp.status_code)

# 1.普通的GET类型接口	GET	/pinter/com/getSku	id=1
def gettest():
    #get 请求 存入param变量
    param ={'id':1}
    rp=sess.request('get', url='http://' + IP + ':8080/pinter/com/getSku', params=param)
    print(rp.text)

# 2.参数为k=v的POST接口	POST	/pinter/com/login	userName=admin&password=1234
def posttest():
    # post 请求 使用data进行传参
    data = {'userName': 'admin','password':'123456'}
    rp = sess.request('post', url='http://' + IP + ':8080/pinter/com/login', data=data)
    print(rp.text)

# 3.参数为json的POST接口	POST	/pinter/com/register	{"userName":"test","password":"1234","gender":1,"phoneNum":"110","email":"beihe@163.com","address":"Beijing"}
def post_json_test():
    # 传json参数，使用json变量进行传参
    json= {"userName":"test","password":"1234","gender":1,"phoneNum":"110","email":"beihe@163.com","address":"Beijing"}
    rp = sess.request('post', url='http://' + IP + ':8080/pinter/com/register', json=json)
    print(rp.text)

# 4.参数为k=json的POST接口	POST	/pinter/com/buy	param={"skuId":123,"num":10}
def post_key_json_test():
    # post请求，使用data进行传参
    data = {'param':{"skuId": 123, "num": 10}}
    rp = sess.request('post', url='http://' + IP + ':8080/pinter/com/buy', data=data)
    print(rp.text)

# 6.返回json数组的GET接口	GET	/pinter/com/userList	genderType=0
def get_value_test():
    # get 请求参数，存入params变量
    param = {"genderType":0}
    rp = sess.request('get', url='http://' + IP + ':8080/pinter/com/userList', params=param)
    print(rp.text)

if __name__ == "__main__":
    gettest()
    posttest()
    post_json_test()
    post_key_json_test()
    get_value_test()



